package com.iman.manjusri.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.common.hibernate.HibernateSupport;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;
import com.iman.manjusri.domain.ArticleBean;
import com.iman.manjusri.domain.MessageDetailBean;
import com.iman.manjusri.mappers.ArticleMapper;
import com.iman.manjusri.repository.IMessageDetailDao;
import com.iman.manjusri.service.ArticlesService;
import com.iman.manjusri.vo.ArticlesVo;
import com.iman.manjusri.vo.CaseVo;
import com.iman.manjusri.vo.ShareExpertVo;



/**
 * 
 * @author Thinkpad
 * 
 */
@Service
public class ArticlesServiceImpl  implements ArticlesService{

	
	 @Inject
    TemplateQLService                      templateQLService;

 
    @Inject
    ArticleMapper  articleMapper;
 
    HibernateSupport                       hibernateSupport;
    
    @Inject
	private IMessageDetailDao messageDetailDaoImpl;
 

	@Override
	public Page<ArticlesVo> listArticles(Map params) {
		 
		 List<ArticlesVo> vos = new ArrayList<ArticlesVo>();
		 
		 /**************   这个是查询数据库
		  * 
		  *       ArticlesVo  这个vo 要有个空参数的构造方法，或者默认的就行
		  * 
		  *       这个作为大家参考的例子  麻烦多写点注视， 还有我下面写的各个方法的说明， 麻烦别删除啊， 别人可以参考
		  * 
		  *       这方法如果想分页  那么需要  sql 里面写 limit 这样 params 传递 分页参数进行分页显示， 这个自己手动也分页等等 ，查询list
	      *       List<ArticlesVo> listObj  =  templateQLService.findBySQLBeanQuery("article-query-list", params, ArticlesVo.class );
		  *	
		  *       这个类需要用hql
		  *       list  =  templateQLService.findByPagedSQLQuery("article-query-list", params, ArticlesVo.class, Page.getStartOfPage(currentPage,pageSize), pageSize);
		  *
		  * 
		  *****************/
		 
		  
		 Integer currentPage = (Integer)params.get( "pageIndex");
		 Integer pageSize  = (Integer)params.get( "pageSize");
		 
		 Integer startNo=(currentPage-1)*pageSize+1;
		// 
		 System.out.println("startNo***********"+startNo);
		 //构造查询参数
		 Map hashMapParams = new HashMap();
		 hashMapParams.put("articleTp",params.get("type"));
		 hashMapParams.put("startNo",startNo);
		 //这个也是用sql  ，支持分页的使用
	      Page<ArticlesVo>  list  = null;
	      list = templateQLService.findByPagedSQLBeanQuery("article-query-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,ArticlesVo.class);
		 
	    
		return  list;
	}

	@Override
	public Page<CaseVo> listCaseByPage(Map params) {
		 
		 
		 Integer currentPage = (Integer)params.get( "pageIndex");
		 Integer pageSize  = (Integer)params.get( "pageSize");
		 /*Integer startNo=(currentPage-1)*pageSize+1;
		 System.out.println("startNo***********"+startNo);
		 //构造查询参数
		 Map hashMapParams = new HashMap();
		 //hashMapParams.put("articleTp",params.get("type"));
		 hashMapParams.put("startNo",startNo);*/
		 //这个也是用sql  ，支持分页的使用
	      Page<CaseVo>  list  = null;
	      list = templateQLService.findByPagedSQLBeanQuery("caseShare-query-list", null, Page.getStartOfPage(currentPage,pageSize), pageSize,CaseVo.class);
		 
	    
		return  list;
	}
	/**
	 * 获取分享达人列表数据  = listCaseByPage
	 * @param pageNum
	 * @param pageSize ; startNo 是分页查询时，当前页的第一条数据行号
	 * @return
	 */
	@Override
	public Page<ShareExpertVo> shareExpertList(int pageNum, int pageSize) {
		int startNo = Page.getStartOfPage(pageNum, pageSize);
		return templateQLService.findByPagedSQLBeanQuery("case-shareExpert-list", null, startNo, pageSize, ShareExpertVo.class);
	}
	//查询TOP最新案例
	@Override
	public Page<CaseVo> listTopNewCase(Map params) {
		 
		// List<ArticlesVo> vos = new ArrayList<ArticlesVo>();
		 
		 Integer currentPage = (Integer)params.get( "pageIndex");
		 Integer pageSize  = (Integer)params.get( "pageSize");
		 //Integer startNo=(currentPage-1)*pageSize+1;
		 //System.out.println("startNo***********"+startNo);
		 //构造查询参数
		 Map hashMapParams = new HashMap();
		 hashMapParams.put("case_tp",params.get("type"));
		 System.out.println("**************"+params.get("type")); 
		 //hashMapParams.put("startNo",startNo);
		 //这个也是用sql  ，支持分页的使用
	      Page<CaseVo>  list  = null;
	      list = templateQLService.findByPagedSQLBeanQuery("caseTopNew-query-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,CaseVo.class);
		 
	    
		return  list;
	}
	//查询热门案例
	@Override
	public Page<CaseVo> listTopHotCase(Map params) {
		// TODO Auto-generated method stub
		 List<ArticlesVo> vos = new ArrayList<ArticlesVo>();
		 
		 Integer currentPage = (Integer)params.get( "pageIndex");
		 Integer pageSize  = (Integer)params.get( "pageSize");
		 Integer startNo=(currentPage-1)*pageSize+1;
		 System.out.println("startNo***********"+startNo);
		 //构造查询参数
		 Map hashMapParams = new HashMap();
		 hashMapParams.put("articleTp",params.get("type"));
		 hashMapParams.put("startNo",startNo);
		 //这个也是用sql  ，支持分页的使用
	      Page<CaseVo>  list  = null;
	      list = templateQLService.findByPagedSQLBeanQuery("caseTopHot-query-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,CaseVo.class);
		 
	    
		return  list;
	}
	
	@Override
	public List<CaseVo> listIndexCase(Map params) {
		 List<CaseVo>  list  = templateQLService.findBySQLBeanQuery("caseTopHot-query-list", params, CaseVo.class);
		return  list;
	}

	//查看案例详情
	@Override
	public Page<CaseVo> getCaseById(Map params) {
		// TODO Auto-generated method stub
		
		 
		 Map hashMapParams = new HashMap();
		 hashMapParams.put("caseId",params.get("caseId"));
		 //更新浏览计数 
		 templateQLService.executeSQLUpdate("case-inc-bcnt", hashMapParams);
		 //这个也是用sql  ，支持分页的使用
	     Page<CaseVo>  list  =  templateQLService.findByPagedSQLBeanQuery("case-byTitle-list", hashMapParams, 1,1,CaseVo.class);
	     String visitor_say_id=list.getData().get(0).getCase_visitor_say(); 
	     String content=findMongoDetailById(visitor_say_id);
	     list.getData().get(0).setCase_visitor_say_c(content);
	     
	     String handle_content=list.getData().get(0).getCase_handle_content(); 
	     String case_handle_content_c=findMongoDetailById(handle_content);
	     System.out.println("case_handle_content_c**********"+case_handle_content_c);
	     list.getData().get(0).setCase_handle_content_c(case_handle_content_c);
	     
	     String case_cure_result=list.getData().get(0).getCase_cure_result(); 
	     String case_cure_result_c=findMongoDetailById(case_cure_result);
	     list.getData().get(0).setCase_cure_result_c(case_cure_result_c);
 		 return  list;
	}

   
	public  String findMongoDetailById (String id){
		
		MessageDetailBean messageDetailBean = messageDetailDaoImpl.findById(id);
		
		if(messageDetailBean!=null){
			return messageDetailBean.getDetailContent();
		}
		
		return "";
	
	}

	
	@Override
	public Page<ArticlesVo> getArticlesById(Map params) {
		// TODO Auto-generated method stub
		
		 Map hashMapParams = new HashMap();
		 hashMapParams.put("articleId",params.get("articleId"));
		 //更新浏览计数
		 templateQLService.executeSQLUpdate("article-inc-bcnt", hashMapParams);
		 //这个也是用sql  ，支持分页的使用
	     Page<ArticlesVo>  list  = null;
	     list = templateQLService.findByPagedSQLBeanQuery("article-byTitle-list", hashMapParams, 1,1,ArticlesVo.class);
	   //  ArticlesVo article = this.hibernateSupport.findUniqueBy(ArticlesVo.class, "title", title);
	     
	     if(list ==null || list.getData()==null  ||list.getData().size()<1){
	    	 return list;
	     }
	     
	     String articleContentId=list.getData().get(0).getArticleContent();
	     
	     String content=findMongoDetailById(articleContentId);
	    
	     list.getData().get(0).setArticleContent(content);
	     
		 return  list;
	}

	@Override
	public List<ArticleBean> getArticleList(Map<String, Object> searchMap) {
		return articleMapper.getArticleInfoBeanList(searchMap);
		
	}

	@Override
	public ArticleBean getArticleInfo(Map<String, Object> searchMap) {
		ArticleBean bean = null;
		List<ArticleBean> articleBeanList = articleMapper.getArticleInfoBeanList(searchMap);
		if(articleBeanList.size()>0){
			bean = articleBeanList.get(0);
		}
		return bean;
	}

	@Override
	public ShareExpertVo commentUserHead(long user_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("user_id", user_id);
		return templateQLService.findObjectByQuery("case-comment-user-info", params, ShareExpertVo.class);
	}
	
	@Override
	public int incrViewCnt(long resource_id) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("resource_id", resource_id);		
		return (Integer) templateQLService.executeSQLUpdate("case-incr-viewcnt", params);
	}

}
